﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Geeno.Common
{
    public class StringUtil
    {
        /// <summary>
        /// 根据指定字节长度，截取对应的字符串信息,默认UTF-8的编码方式
        /// </summary>
        /// <param name="input">输入内容</param>
        /// <param name="byteLength">字节长度</param>
        /// <returns></returns>
        public static string SubStringBytes(string input, int byteLength)
        {
            string rlt = string.Empty;
            if (Encoding.UTF8.GetBytes(input).Length > byteLength)
            {
                int len = 0;
                StringBuilder strTmp = new StringBuilder();
                foreach (char item in input)
                {
                    len = len + Encoding.UTF8.GetBytes(item.ToString()).Length;
                    if (len > byteLength)
                        break;
                    else
                        strTmp.Append(item);
                }
                rlt = strTmp.ToString();
            }
            return rlt;
        }
    }
}
